package ca.ualberta.cs.PrivacyManagement;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryptor {

	public static String toHexString(byte[] b) {
	      char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
	                         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	      StringBuffer buf = new StringBuffer();
	      for (int j=0; j<b.length; j++) {
	         buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
	         buf.append(hexDigit[b[j] & 0x0f]);
	      }
	      return buf.toString();
	   }
	
	public static String encryptOrAnonymize(String origVal){
		if(origVal == null)
			return null;
		
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(origVal.getBytes());
			byte[] output = md.digest();
			return toHexString(output);			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "notapass";
		}
	}
	
	public static boolean encryptedValsMatch(String newvalOrig, String oldvalHex){
		return encryptOrAnonymize(newvalOrig).equals(oldvalHex);		
	}
	
}
